0

Right now I'm writing a little, simple space game with PyGame. I started with the background and a movable player, that also shoots. So far so good. Thursday I started and was pretty successful. Everything I wanted achieve to this point worked. So I started creating the asteroids ... until a day later I recognized that my player suddenly leaves the window!

I did'nt altered that part of the code (from row 121) - and I can't see a problem with it! :( Why does the player suddenly started acting like that? What did I wrong?

#! /usr/bin/env python
# coding: utf-8
# Created on 09.12.2022

# @author: norman

import random
import pygame

pygame.init()
clock = pygame.time.Clock()

gameactive = True

# =============
# Variablen/Aufbau
# =============

# Fenster
windx = 1920
windy = 1080
screen = pygame.display.set_mode((windx, windy))
background = pygame.image.load("C:\\Users\\norman\\Desktop\\python\\spacegame\\bgspace.jpg")
pygame.display.set_caption("SPACE GAME")

# Raumschiff/Spieler

playerImg = pygame.image.load("C:\\Users\\norman\\Desktop\\python\\spacegame\\ship.png")
playerx = windx/10
playery = windy/2
playermovx = 0
playermovy = 0
playerspeed = 8


def player(x, y):
    screen.blit(playerImg, (x, y))


# Laser

laserImg = pygame.image.load("C:\\Users\\norman\\Desktop\\python\\spacegame\\laser.png")
laserx = 0
lasery = 0
lasermov = 0
laserspeed = 20
laserstat = 0


def laser(x, y):
    global laserstat
    laserstat = 1
    screen.blit(laserImg, (x, y))


# Asteroiden

astnum = 2

astimgs = []
astimg1 = pygame.image.load("C:\\Users\\norman\\Desktop\\python\\spacegame\\ast1.png")
astimg2 = pygame.image.load("C:\\Users\\norman\\Desktop\\python\\spacegame\\ast2.png")
astimg3 = pygame.image.load("C:\\Users\\norman\\Desktop\\python\\spacegame\\ast3.png")
astimgs.append(astimg1)
astimgs.append(astimg2)
astimgs.append(astimg3)

astx = 0
asty = 0
astmov = 0
astspeed = 20

# for i in astnum:


# =============
# Spielroutine
# =============

while gameactive:

    screen.blit(background, (0, 0))

    for event in pygame.event.get():

        if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            gameactive = False
            print("Das Spiel wurde beendet!")

    # Bewegung des Spielers + schießen (+ Tastenbefehle)

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_UP:
                playermovy = -playerspeed
            elif event.key == pygame.K_DOWN:
                playermovy = playerspeed
            elif event.key == pygame.K_RIGHT:
                playermovx = playerspeed
            elif event.key == pygame.K_LEFT:
                playermovx = -playerspeed

            if event.key == pygame.K_SPACE:
                if laserstat == 0:
                    laserx = playerx+200
                    lasery = playery+13
                    laser(laserx, lasery)

        if event.type == pygame.KEYUP:

            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                playermovy = 0
            elif event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                playermovx = 0

    if playermovx != 0:
        playerx += playermovx
    if playermovy != 0:
        playery += playermovy

    if playerx <= 0:
        playerx = 0
    if playerx >= windx-200:
        playerx = windx-200
    if playery <= 0:
        playery = 0
    if playery >= windy-53:
        playery = windy-53

    if laserx > windx:
        laserx = 0
        laserstat = 0
    if laserstat == 1:
        laser(laserx, lasery)
        laserx += laserspeed

    player(playerx, playery)
    pygame.display.flip()
    clock.tick(120)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
nostu
  • 1

0 Answers0