Hi all, I’m a beginner using python3, and recently learnt oop in pygame. What I’m trying to create is a tug of war game, but somehow my console prints Ai wins even before the player crosses the middle line.Here is my code:
newsettings.py: # Basic colours BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) GREEN = ( 0, 255, 0) RED = ( 255, 0, 0) BLUE = ( 0, 0, 255) YELLOW = ( 255, 255, 0) ORANGE = ( 255, 165, 0) PURPLE = ( 100, 0, 100) #settings WIDTH = 1024 HEIGHT = 768 FPS = 60 TITLE = "War of Tugs" BGCOLOR = GREEN spritesdata.py: import pygame as pg from os import path import os from newsettings import * size = (WIDTH, HEIGHT) screen = pg.display.set_mode(size) pg.display.init() game_folder = os.path.dirname(__file__) resources_folder = os.path.join(game_folder, 'resources') stickmanplayer_img = pg.image.load(os.path.join(resources_folder, 'stickmanplayer.png')).convert() stickmanai_img = pg.image.load(os.path.join(resources_folder, 'stickmanai.png')).convert() #https://www.shutterstock.com/image-vector/stick-figure-pulling-rope-coiled-1140317804?src=c7Vbr97B4rIRsJ9OYUVcLw-1-0 string = pg.sprite.Group() all_sprites = pg.sprite.Group() stickman_ai = pg.sprite.Group() stickman_player = pg.sprite.Group() AI_wins = 0 Player_wins = 0 class Player(pg.sprite.Sprite): def __init__(self, game, x, y): self.groups = game.all_sprites pg.sprite.Sprite.__init__(self, self.groups) self.game = game self.image = stickmanplayer_img self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.dx = 1 def move_left(self,lol): if self.rect.left > 10 and lol.rect.x > 310: self.rect.x -= 30 lol.rect.x -= 30 else: self.rect.left = 10 def update(self): if self.rect.right < 770: self.rect.x += self.dx class AI(pg.sprite.Sprite): def __init__(self, game, x, y): self.groups = game.all_sprites pg.sprite.Sprite.__init__(self, self.groups) self.game = game self.image = stickmanai_img self.image.set_colorkey(BLACK) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.dx = 1 def update(self): if self.rect.right < 1000: self.rect.x += self.dx class String(pg.sprite.Sprite): def __init__(self, game, x, y, height = 10, width = 320): self.game = game self.groups = game.all_sprites pg.sprite.Sprite.__init__(self, self.groups) self.height = height self.width = width self.surface = pg.Surface((2 * self.width, 2 * self.height)) self.surface.fill(YELLOW) self.surface.set_colorkey(YELLOW) pg.draw.line(self.surface, BLACK, [0,0], [self.width,0], self.height) self.image = self.surface self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.dx = 1 def move_to_player(self): if self.rect.left > 100: self.rect.x -= 30 else: self.rect.left = 100 def update(self): if self.rect.right < 1300: self.rect.x += self.dx def check_win(self,lol): global Player_wins global AI_wins if self.rect.right < lol.rect.x: Player_wins += 1 print("player wins") if self.rect.left > lol.rect.x: AI_wins += 1 print("ai wins") class Middle_Line(pg.sprite.Sprite): def __init__(self, game, x): self.game = game self.groups = game.all_sprites pg.sprite.Sprite.__init__(self, self.groups) self.width = WIDTH + 100 self.height = HEIGHT self.surface = pg.Surface((2 * self.width, 2 * self.height)) self.surface.fill(WHITE) self.surface.set_colorkey(WHITE) pg.draw.line(self.surface, RED, [200, 0], [200, self.height], 5) self.image = self.surface self.rect = self.image.get_rect() self.rect.x = x main.py: import pygame as pg import sys import random import math import time from os import path from newsettings import * from spritesdata import * clock = pg.time.Clock() Player_wins = 0 AI_wins = 0 class Game: def __init__(self): pg.init() Player_wins = 0 AI_wins = 0 self.screen = pg.display.set_mode((WIDTH, HEIGHT)) pg.display.set_caption(TITLE) self.clock = pg.time.Clock() self.time = pg.time.get_ticks() pg.key.set_repeat(500, 100) self.all_sprites = pg.sprite.Group() self.player = Player(self, 249, 384) self.ai = AI(self, 550, 430) self.middle_line = Middle_Line(self, 300) self.string = String(self, 350, 500) def run(self): # game loop - set self.playing = False to end the game self.playing = True while self.playing: self.dt = self.clock.tick(FPS) / 1000 self.events() self.update() self.draw() def quit(self): pg.quit() sys.exit() def update(self): self.all_sprites.update() self.string.check_win(self.middle_line) self.all_sprites.update() def draw(self): self.screen.fill(BGCOLOR) self.all_sprites.draw(self.screen) font = pg.font.SysFont('Arial', 30, True, False) text = font.render("PLAYER WINS:" + str(Player_wins), True, BLACK) screen.blit(text, [50, 50]) font = pg.font.SysFont('Arial', 30, True, False) text = font.render("AI WINS:" + str(AI_wins), True, BLACK) screen.blit(text, [790, 50]) self.all_sprites.update() pg.display.flip() def events(self): # catch all events here for event in pg.event.get(): if event.type == pg.QUIT: self.quit() Apologise for the absence of > signs(im using atom _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor